package com.zyao89.view.zloading.circle;

import com.zyao89.view.zloading.ZLoadingBuilder;
import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/4/24.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: https://zyao89.cn
 */
public class RotateCircleBuilder extends ZLoadingBuilder {
    private static final int CIRCLE_NUM = 10;
    private Paint mFullPaint;
    private float mOuterRadius;
    private float mDefaultAngle;
    private float mInterRadius;

    @Override
    protected void initParams(Context context, Color color) {
        mOuterRadius = getAllSize();
        initPaint(color);
        mInterRadius = dip2px(context, 2);
    }

    private void initPaint(Color color) {
        mFullPaint = new Paint();
        mFullPaint.setStyle(Paint.Style.FILL_STYLE);
        mFullPaint.setColor(color);
        mFullPaint.setDither(true);
        mFullPaint.setFilterBitmap(true);
        mFullPaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mFullPaint.setStrokeJoin(Paint.Join.ROUND_JOIN);
    }

    private void drawCircles(Canvas canvas) {
        int all = CIRCLE_NUM;
        for (int i = 0; i < all; i++) {
            int angle = (int) NumCalcUtil.add(360 / all * i, mDefaultAngle);
            float centerX = NumCalcUtil.add(getViewCenterX(), mOuterRadius * cos(angle));
            float centerY = NumCalcUtil.add(getViewCenterY(), mOuterRadius * sin(angle));
            //mFullPaint.setAlpha(255 / all * i);
            canvas.drawCircle(centerX, centerY, NumCalcUtil.add(i, mInterRadius), mFullPaint);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawCircles(canvas);
    }


    @Override
    protected void setAlpha(int alpha) {
        mFullPaint.setAlpha(alpha);
    }

    @Override
    protected void prepareStart(AnimatorValue floatValueAnimator) {

    }

    @Override
    protected void prepareEnd() {

    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
        mDefaultAngle = 360 * animatedValue;
    }

    @Override
    protected void setColorFilter(ColorFilter colorFilter) {
        mFullPaint.setColorFilter(colorFilter);
    }

    protected final float cos(int num) {
        return (float) Math.cos(num * Math.PI / 180);
    }

    protected final float sin(int num) {
        return (float) Math.sin(num * Math.PI / 180);
    }

    @Override
    public void onRepeat(Animator animator) {

    }
}
